package com.bothin.experiment4.service.impl;

import com.bothin.experiment4.converter.ContactForm2ContactConverter;
import com.bothin.experiment4.dto.ContactDTO;
import com.bothin.experiment4.enums.ContactStatusEnum;
import com.bothin.experiment4.form.ContactForm;
import com.bothin.experiment4.model.Customer;
import com.bothin.experiment4.model.Employee;
import com.bothin.experiment4.model.Contact;
import com.bothin.experiment4.service.IContactService;
import com.bothin.experiment4.service.base.BaseServiceImpl;
import com.bothin.experiment4.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by bothin
 * At 2019/12/17 10:54
 * Description:
 */
@Service
public class ContactServiceImpl extends BaseServiceImpl<Contact> implements IContactService {

    @Override
    public Contact findById(Object id) {
        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Contact> findAll() {
        return contactMapper.selectAll();
    }

    @Override
    @Transactional
    public void deleteById(Object id) {
        contactMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Contact contact) {
        contactMapper.updateByPrimaryKeySelective(contact);
    }

    @Override
    @Transactional
    public void insert(Contact contact) {
        contactMapper.insertSelective(contact);
    }

    /**
     * 转换DTO的方法 converter
     * @param contact
     * @return
     */
    public ContactDTO converter(Contact contact){
        ContactDTO contactDTO = new ContactDTO();
        if (contact!=null){
            BeanUtils.copyProperties(contact,contactDTO);
            Customer customer = customerMapper.selectByPrimaryKey(contactDTO.getCustomerId());
            if (customer!=null)
                contactDTO.setCustomerName(customer.getCustomerName());
            Employee employee = employeeMapper.selectByPrimaryKey(contactDTO.getEmployeeId());
            if (employee!=null)
                contactDTO.setEmployeeName(employee.getName());
        }
        return contactDTO;
    }

    public List<ContactDTO> converterAll(List<Contact> contacts){
        return contacts.stream().map(e->converter(e)).collect(Collectors.toList());
    }

    @Override
    public List<ContactDTO> findByKeywordDTO(String key) {
        return contactMapper.findByKeywordDTO("%"+key+"%");
    }

    /**
     * 查找封装ContactDTO
     * @return
     */
    @Override
    public List<ContactDTO> findALLDTO() {
        /*List<Contact> contacts = contactMapper.selectAll();
        return converterAll(contacts);*/
        return contactMapper.findALLDTO();
    }

    @Override
    public List<ContactDTO> findAllByPerformanceStatusDTO(Integer status) {
        return contactMapper.findAllByPerformanceStatusDTO(status);
    }

    @Override
    public ContactDTO findByIdDTO(String contactId) {
//        return converter(contactMapper.selectByPrimaryKey(contactId));
        return contactMapper.findByIdDTO(contactId);
    }

    @Override
    public List<ContactDTO> findByEmployeeIdDTO(Integer employeeId) {
        return contactMapper.findByEmployeeIdDTO(employeeId);
    }

    @Override
    public List<ContactDTO> findByEmployeeIdAndKeywordDTO(Integer employeeId, String key) {
        return contactMapper.findByEmployeeIdAndKeywordDTO(employeeId,"%"+key+"%");
    }

    @Override
    @Transactional
    public void insertByForm(ContactForm contactForm) throws ParseException {
        insert(ContactForm2ContactConverter.converter(contactForm));
    }

    @Override
    @Transactional
    public void updateByForm(ContactForm contactForm) throws ParseException {
        update(ContactForm2ContactConverter.converter(contactForm));
    }

    @Override
    public void updatePerformanceStatusById(String contactId,Integer status) {
        Contact contact = findById(contactId);
        if (contact!=null){
            contact.setPerformanceStatus(status);
            update(contact);
        }
    }
}
